package cn.admin.interceptor;

import cn.admin.config.key.SessionKey;
import cn.admin.entity.Admin;
import cn.admin.entity.Power;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 11:03 2019/3/23
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {



    /**
     * This implementation always returns {@code true}.
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String project="/loan_admin";
        String url = request.getRequestURI();   //得到访问的URL
        HttpSession session = request.getSession();     //得到session对象
        //判断是否是静态文件
        if (url.contains("static") || url.contains("login") || url.contains("api") || url.contains("img")) {
            return true;
        }


        Admin admin= (Admin) session.getAttribute(SessionKey.SESSION_NAME_ADMIN);
        if (null==admin){
            response.sendRedirect(project+"/admin/login");
        }else {
            List<Power> power= (List<Power>) session.getAttribute(SessionKey.SESSION_NAME_POWER);
            for (Power power1 : power) {
                System.out.println(power1.getPower_url());
            }
            for (Power p : power) {
                if (url.contains(p.getPower_url())){
                    return true;
                }
            }
        }
        return false;
    }



}
